package com.fr.report.io.core;

import com.fr.base.FRFont;
import com.fr.base.Inter;
import com.fr.base.Style;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.report.CellElement;
import com.fr.report.RWorkBook;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import com.fr.report.WorkBook;
import com.fr.report.WorkSheet;
import com.fr.report.core.PaintUtils;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.io.xml.SynchronizedFrozenColumnRow;
import com.fr.report.io.xml.SynchronizedNameWidget;
import com.fr.report.io.xml.SynchronizedReportSettings;
import com.fr.report.io.xml.SynchronizedStyleList;
import com.fr.report.io.xml.SynchronizedVersion;
import com.fr.util.Utils;
import java.awt.Color;
import java.awt.Font;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.Format;
import java.util.Iterator;

public class IOHelper
{
  public static void innerExportReport(Report paramReport, OutputStream paramOutputStream, char paramChar, String paramString)
  {
    PrintWriter localPrintWriter;
    try
    {
      localPrintWriter = new PrintWriter(new OutputStreamWriter(paramOutputStream, paramString));
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      localUnsupportedEncodingException.printStackTrace();
      localPrintWriter = new PrintWriter(paramOutputStream);
    }
    StringBuffer localStringBuffer = null;
    int i = 0;
    int j = 0;
    Iterator localIterator = paramReport.cellIterator();
    while (true)
    {
      String str;
      int k;
      while (true)
      {
        if (!(localIterator.hasNext()))
          break label285;
        CellElement localCellElement = (CellElement)localIterator.next();
        Object localObject = localCellElement.getValue();
        if (localObject == null)
          localObject = "";
        Style localStyle = localCellElement.getStyle();
        Format localFormat = localStyle.getFormat();
        str = PaintUtils.valueToText(localObject, localFormat);
        k = localCellElement.getColumn();
        int l = localCellElement.getRow();
        if (l <= i)
          break;
        if (localStringBuffer != null)
          localPrintWriter.println(localStringBuffer.toString());
        for (i1 = i; i1 < l; ++i1)
          localPrintWriter.println();
        localStringBuffer = new StringBuffer();
        i = l;
        j = 0;
        for (i1 = j; i1 < k; ++i1)
          localStringBuffer.append(paramChar);
        localStringBuffer.append(str);
        j = k;
      }
      localStringBuffer = new StringBuffer();
      for (int i1 = j; i1 < k; ++i1)
        localStringBuffer.append(paramChar);
      localStringBuffer.append(str);
      j = k;
    }
    if (localStringBuffer != null)
      label285: localPrintWriter.print(localStringBuffer.toString());
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static String createCSSFont(Font paramFont)
  {
    if (paramFont == null)
      return "";
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("font-family:").append(paramFont.getName()).append(";");
    localStringBuffer.append("font-size:").append(paramFont.getSize()).append(";");
    if (paramFont.isBold())
      localStringBuffer.append("font-weight:bold;");
    if (paramFont.isItalic())
      localStringBuffer.append("font-style:italic;");
    if (paramFont instanceof FRFont)
    {
      FRFont localFRFont = (FRFont)paramFont;
      localStringBuffer.append("color:").append(Utils.javaColorToCSSColor(localFRFont.getForeground())).append(";");
      localStringBuffer.append("text-decoration:");
      if ((!(localFRFont.isStrikethrough())) || (localFRFont.getUnderline() == 0))
      {
        localStringBuffer.append(" none");
      }
      else
      {
        if (localFRFont.isStrikethrough())
          localStringBuffer.append(" line-through ");
        if (localFRFont.getUnderline() != 0)
          localStringBuffer.append(" underline");
      }
      localStringBuffer.append(";");
      if ((localFRFont.isSubscript()) || (localFRFont.isSuperscript()))
        localStringBuffer.append("font-variant:small-caps;");
    }
    return localStringBuffer.toString();
  }

  public static String createCSSBorder(int paramInt1, int paramInt2, Color paramColor)
  {
    return createCSSBorder(paramInt1, paramInt2, Utils.javaColorToCSSColor(paramColor));
  }

  public static String createCSSBorder(int paramInt1, int paramInt2, String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    String str = "border";
    if (paramInt1 == 1)
      str = str + "-top";
    else if (paramInt1 == 4)
      str = str + "-right";
    else if (paramInt1 == 3)
      str = str + "-bottom";
    else if (paramInt1 == 2)
      str = str + "-left";
    localStringBuffer.append(str).append("-color:").append(paramString).append(";");
    if (paramInt2 == 0)
    {
      localStringBuffer.append(str).append("-style:solid;");
      localStringBuffer.append(str).append("-width:0px;");
    }
    else if (paramInt2 == 1)
    {
      localStringBuffer.append(str).append("-style:solid;");
      localStringBuffer.append(str).append("-width:1px;");
    }
    else if (paramInt2 == 2)
    {
      localStringBuffer.append(str).append("-style:solid;");
      localStringBuffer.append(str).append("-width:2px;");
    }
    else if (paramInt2 == 6)
    {
      localStringBuffer.append(str).append("-style:double;");
      localStringBuffer.append(str).append("-width:2px;");
    }
    else if ((paramInt2 == 3) || (paramInt2 == 4) || (paramInt2 == 14))
    {
      localStringBuffer.append(str).append("-style:dashed;");
      localStringBuffer.append(str).append("-width:1px;");
    }
    else if (paramInt2 == 5)
    {
      localStringBuffer.append(str).append("-style:solid;");
      localStringBuffer.append(str).append("-width:3px;");
    }
    else if (paramInt2 == 7)
    {
      localStringBuffer.append(str).append("-style:dotted;");
      localStringBuffer.append(str).append("-width:1px;");
    }
    else if ((paramInt2 == 8) || (paramInt2 == 10) || (paramInt2 == 12) || (paramInt2 == 13))
    {
      localStringBuffer.append(str).append("-style:dashed;");
      localStringBuffer.append(str).append("-width:2px;");
    }
    else if ((paramInt2 == 9) || (paramInt2 == 11))
    {
      localStringBuffer.append(str).append("-style:dotted;");
      localStringBuffer.append(str).append("-width:2px;");
    }
    return localStringBuffer.toString();
  }

  public static int getBorderWidth(int paramInt)
  {
    switch (paramInt)
    {
    case 0:
      return 0;
    case 1:
      return 1;
    case 3:
      return 1;
    case 4:
      return 1;
    case 14:
      return 1;
    case 5:
      return 3;
    case 7:
      return 1;
    case 2:
    case 6:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    }
    return 2;
  }

  public static WorkBook generateTemplate(InputStream paramInputStream, String paramString)
    throws Exception
  {
    if (paramInputStream == null)
      throw new Exception(Inter.getLocText("Cannot_Found_Template_File") + ".");
    InputStreamReader localInputStreamReader = new InputStreamReader(paramInputStream, paramString);
    XMLableReader localXMLableReader = XMLableReader.createXMLableReader(localInputStreamReader);
    String str1 = localXMLableReader.getTagName();
    if (str1 == null)
      throw new Exception(Inter.getLocText("Error_Template_File") + ".");
    WorkBook localWorkBook = null;
    if (str1.equals("WorkBook"))
    {
      localWorkBook = new WorkBook();
      localXMLableReader.readXMLObject(localWorkBook);
    }
    else if (str1.equals("Report"))
    {
      String str2;
      Object localObject = null;
      if ((str2 = localXMLableReader.getAttr("class")) != null)
      {
        Report localReport = ReportXMLUtils.readReportFromClassName(str2);
        if (localReport instanceof TemplateReport)
          localObject = (TemplateReport)localReport;
        else
          throw new Exception("Error Report Class (" + localReport.getClass().getName() + ") in Template File.");
      }
      if (localObject == null)
        localObject = new WorkSheet();
      localWorkBook = new WorkBook();
      localWorkBook.addReport("Compatible0651WorkSheet", (TemplateReport)localObject);
      localXMLableReader.readXMLObject((XMLReadable)localObject);
      localWorkBook.setReportName(0, "sheet1");
    }
    SynchronizedReportSettings.flushSynchronizedReportSettings();
    SynchronizedFrozenColumnRow.flushSynchronizedFrozenColumnRow(localWorkBook);
    SynchronizedStyleList.flush_current_thread_styles();
    SynchronizedStyleList.removeSynchronizedCellStyleList(Thread.currentThread());
    SynchronizedNameWidget.removeCurrent();
    SynchronizedVersion.removeSynchronizedVersion(Thread.currentThread());
    localXMLableReader.close();
    return ((WorkBook)localWorkBook);
  }

  public static RWorkBook generateResult(InputStream paramInputStream, String paramString)
    throws Exception
  {
    if (paramInputStream == null)
      throw new Exception(Inter.getLocText("Cannot_Found_Template_File") + ".");
    InputStreamReader localInputStreamReader = new InputStreamReader(paramInputStream, paramString);
    XMLableReader localXMLableReader = XMLableReader.createXMLableReader(localInputStreamReader);
    String str = localXMLableReader.getTagName();
    if (str == null)
      throw new Exception(Inter.getLocText("Error_Template_File") + ".");
    RWorkBook localRWorkBook = null;
    if ((str.equals("RWorkBook")) || (str.equals("WorkBook")))
    {
      localRWorkBook = new RWorkBook();
      localXMLableReader.readXMLObject(localRWorkBook);
    }
    SynchronizedStyleList.flush_current_thread_styles();
    SynchronizedStyleList.removeSynchronizedCellStyleList(Thread.currentThread());
    SynchronizedNameWidget.removeCurrent();
    SynchronizedVersion.removeSynchronizedVersion(Thread.currentThread());
    localXMLableReader.close();
    return localRWorkBook;
  }
}